package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.MarketBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.model.entity.marketing.coupon.CouponRuleInfoEntity;
import com.hst.capacity.domain.model.entity.marketing.promotion.PromotionEntity;
import com.hst.capacity.domain.model.entity.marketing.promotion.PromotionRelationEntity;
import com.hst.capacity.domain.model.request.marketing.MarketSearchParam;
import com.hst.capacity.domain.model.request.marketing.coupon.CouponRuleParam;
import com.hst.capacity.domain.model.request.marketing.promotion.PromotionParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.marketing.promotion.PromotionVO;
import com.hst.capacity.domain.model.response.setting.price.ExpressSearchVO;
import com.hst.capacity.infra.convertor.MarketConvertor;
import com.hst.capacity.infra.persistence.jpa.marketing.coupon.CouponRuleInfoRepository;
import com.hst.capacity.infra.persistence.jpa.marketing.promotion.PromotionRelationRepository;
import com.hst.capacity.infra.persistence.jpa.marketing.promotion.PromotionRepository;
import com.hst.capacity.infra.util.DateUtil;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketBizImpl implements MarketBiz {

    @Autowired
    private PromotionRepository pRepository;
    @Autowired
    private PromotionRelationRepository prRepository;
    @Autowired
    private CouponRuleInfoRepository crRepository;

    @Override
    public PagingResultVO<PromotionVO> findPromotionListByIdFranchisee(MarketSearchParam param) {
//        if(StringUtil.isBlank(param.getStartDate())) {
        Page<PromotionEntity> page = pRepository.findPromotionEntityByIdFranchisee(param.getIdFranchisee(), PageRequest.of(param.getPageIndex()-1, param.getPageSize()));
        return new PagingResultVO<>(page.getTotalElements(), MarketConvertor.INSTANCE.convert2LstVO(page.getContent()));
//        } else {
//            LocalDateTime startDate = DateUtil.parseLocalDateTime(param.getStartDate());
//            LocalDateTime endDate = DateUtil.parseLocalDateTime(param.getEndDate());
//            return pRepository.findByIdFranchisee(param.getIdFranchisee(),startDate,endDate,param.getPopFlg(),param.getEnabled());
//        }
    }

    @Override
    public boolean addOrUpdPromotion(PromotionParam param) {
        PromotionEntity e = MarketConvertor.INSTANCE.convert(param);
        if(StringUtil.isBlank(e.getIdPromotion())){
            e.setIdPromotion(StringUtil.generateUuid());
        }
        e.setCreateBy(Constants.Def_CreateBy);
        pRepository.save(e);
        return true;
    }

//    @Override
//    public boolean delPromotion(String idPromotion) {
//        pRepository.deleteById(idPromotion);
//        prRepository.deleteByIdPromotion(idPromotion);
//        return true;
//    }

    @Override
    public List<CouponRuleInfoEntity> findCouponRuleListByIdFranchisee(MarketSearchParam param) {
        if(StringUtil.isBlank(param.getStartDate())){
            return crRepository.findByIdFranchisee(param.getIdFranchisee(),param.getEnabled());
        }else{
            LocalDateTime startDate = DateUtil.parseLocalDateTime(param.getStartDate());
            LocalDateTime endDate = DateUtil.parseLocalDateTime(param.getEndDate());
            return crRepository.findByIdFranchisee(param.getIdFranchisee(),startDate,endDate,param.getEnabled());
        }
    }

    @Override
    public boolean addOrUpdCouponRule(CouponRuleParam param) {
        CouponRuleInfoEntity e = MarketConvertor.INSTANCE.convert(param);
        e.setCreateBy(Constants.Def_CreateBy);
        if(StringUtil.isBlank(e.getIdCouponRule())){
            e.setIdCouponRule(StringUtil.generateUuid());
        }
        crRepository.save(e);
        return true;
    }

//    @Override
//    public boolean delCouponRule(String idCouponRule) {
//        crRepository.deleteById(idCouponRule);
//        return true;
//    }

    @Override
    public List<CouponRuleInfoEntity> findCouponRuleListByPromotion(String idFranchisee, String idPromotion, int bandKbn) {
        // 0:未绑定;1：绑定
        if (Constants.BindKbn.unBinding==bandKbn){
            return crRepository.findUnBandCouponRuleList(idFranchisee,idPromotion);
        }else{
            return crRepository.findBandCouponRuleList(idFranchisee,idPromotion);
        }
    }

    @Override
    public boolean bindCouponRule(String idPromotion, String idCouponRule, int bandKbn) {
        boolean ret = false;
        // 0:未绑定;1：绑定
        if (Constants.BindKbn.Binding==bandKbn){
            PromotionRelationEntity e = new PromotionRelationEntity();
            e.setIdPromotion(idPromotion);
            e.setIdCouponRule(idCouponRule);
            e.setCreateBy(Constants.Def_CreateBy);
            prRepository.save(e);
            ret = true;
        }else{
            prRepository.deleteByIdPromotionAndAndIdCouponRule(idPromotion,idCouponRule);
            ret = true;
        }
        return ret;
    }

}
